using System.Collections.Generic;
using System.Configuration;
using System.Drawing;

namespace OrbitOne.Elmah.BL
{
    public class ApplicationHealthStates
    {
        #region Public Static Member
        public static Color[] HealthStatusColors = new[]{
                                                            Color.Green, Color.Lime, Color.LawnGreen, Color.YellowGreen, 
                                                            Color.Yellow, Color.Orange, Color.OrangeRed, Color.Red
                                                        }; 
        #endregion

        #region Private Static Properties
        private static int _dayCriticalHealthStatusIndex = -1;
        private static int DayCriticalHealthStatusIndex
        {
            get
            {
                if (_dayCriticalHealthStatusIndex == -1)
                    if (!int.TryParse(ConfigurationManager.AppSettings["CriticalHealthStatusIndex"], out _dayCriticalHealthStatusIndex))
                        _dayCriticalHealthStatusIndex = 50;
                return _dayCriticalHealthStatusIndex;
            }
        } 
        #endregion

        #region Private static Method
        private static Color GetColor(double status, double criticalIndex)
        {
            var colorIndex = (int)((status / criticalIndex) * HealthStatusColors.Length);
            return colorIndex < HealthStatusColors.Length
                       ? HealthStatusColors[colorIndex]
                       : HealthStatusColors[HealthStatusColors.Length - 1];
        }
        #endregion

        #region Public Properties
        public string ApplicationName { get; set; }

        public double DayState { get; private set; }
        public double TwoDayState { get; private set; }
        public double WeekState { get; private set; }
        public double MonthState { get; private set; }
        public double YearState { get; private set; }

        public Color DayColor { get { return GetColor(DayState, DayCriticalHealthStatusIndex); } }
        public Color TwoDayColor { get { return GetColor(TwoDayState, DayCriticalHealthStatusIndex * 1.5); } }
        public Color WeekColor { get { return GetColor(WeekState, DayCriticalHealthStatusIndex * 2.75); } }
        public Color MonthColor { get { return GetColor(MonthState, DayCriticalHealthStatusIndex * 5.625); } }
        public Color YearColor { get { return GetColor(YearState, DayCriticalHealthStatusIndex * 26.5625); } } 
        #endregion

        #region Constructor
        public ApplicationHealthStates(string name, double day, double twoDay, double week, double month, double year)
        {
            ApplicationName = name;
            DayState = day;
            TwoDayState = DayState + twoDay;
            WeekState = TwoDayState + week;
            MonthState = WeekState + month;
            YearState = MonthState + year;
        } 
        #endregion

        #region Public Static Methods
        public static IList<ApplicationHealthStates> GetAll()
        {
            return OrbitOneElmahErrorService.GetApplicationHealthStates();
        }
        #endregion
    }
}